package com.ruoyi.common.utils;

import java.util.*;

public class ShuffleStringUtils {
    public static char[] stringToCharArray(String str) {
        if (str == null) {
            return new char[0];
        }

        char[] charArray = new char[str.length()];
        for (int i = 0; i < str.length(); i++) {
            charArray[i] = str.charAt(i);
        }

        return charArray;
    }

    public static void shuffle(char[] array) {
        List<Character> charList = new ArrayList<>();

        for (char c : array) {
            charList.add(c);
        }

        Collections.shuffle(charList);

        for (int i = 0; i < charList.size(); i++) {
            array[i] = charList.get(i);
        }
    }

    public static String get(String exampleString) {
        // 将字符串转为字符数组
        char[] charArray = stringToCharArray(exampleString);

        // 打乱字符数组的顺序
        shuffle(charArray);

        StringBuilder stringBuilder = new StringBuilder();
        // 输出正确顺序的索引
        for (int i = 0; i < exampleString.length(); i++) {
            char c = exampleString.charAt(i);
            int index = new String(charArray).indexOf(c);
            String formattedNumber = String.format("%04d", index);
            stringBuilder.append(formattedNumber);
        }
        System.out.println("key:" + stringBuilder.toString());

        StringJoiner stringJoiner = new StringJoiner(",");
        for (char c : charArray) {
            stringJoiner.add("'" + c + "'");
        }
        System.out.println("Shuffled character array: " + stringJoiner.toString());
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        String exampleString = "https://ne_token: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@github.com/steve940/maven-exec.git";

         System.out.println(      get(exampleString));

        String key = "00050029002900000013003900020002003400070010002900080043000700340039000300050000001000070012001200650070000400490079000100360053003500320040003100340004003800160011001700080006008000270005000600060035001700220028003600330015006600470003002000290005003100850062006900080028000200130029000700010007004000040017000200290007001300290062000300200029";
        char[] charArray = new char[]{'p', 'v', '/', 'g', '4', 'h', 'F', 'e', 'o', 'h', '_', 'L', 'w', 's', 'h', 'Q', 'V', '0', '0', '4', 'i', 'F', '3', 'e', '4', '/', 'e', 'H', 'm', 't', '_', 'u', 'T', 'B', 'n', 'E', 'K', 'e', 'Z', ':', '9', 'u', 't', 'k', 'h', 'p', '0', '@', 't', 'D', 't', '/', 'v', '7', '9', 'g', 'o', 't', 's', ':', 's', 'o', '.', 'F', 'n', 'X', 'J', 'n', 't', 'c', 'O', 'e', 'K', 't', 'i', 'g', 'e', 'w', 'E', '8', 'C', 't', '.', '/', 'm', 'b'};
        String key2 = "00240017001700600037003500020002000700000018001700030022000000070035002600240060001800000082008200740069000100810052000800330045003800490043003600070001004100800039004800030015000900100024001500150038004800770030003300040046008500400026001200170024003600190020002100030030000200370017000000080000004300010048000200300005000800000007005700000006000000210020002600120017";
        char[] charArray2 = new char[] {'e','4','/','o','B','a','x','n','v','C','H','e','i','n','v','F','e','t','_','b','.','c','k','e','h','F','g','/','h','t','m','n','h','K','o',':','u','s','E','L','@','Z','t','9','t','7','Q','c','0','T','K','i','8','n','e','/',':','-','0','9','p','g','4','m','_','h','o','.','e','O','v','p','e','u','X','t','0','3','s','g','V','D','w','w','t','J','m','/','F','E','e','4'};
        String reconstructedString = decode(key2, charArray2);

        if (reconstructedString.toString().equals(exampleString)) {
            System.out.println("Reconstructed string: " + reconstructedString.toString());
            System.out.println("成功");
        }
    }

    private static String decode(String k, char[] c) {
        int len = k.length();
        StringBuilder s = new StringBuilder(len / 4); // 预估结果长度，提高性能
        for (int i = 0; i < len; i += 4) {
            int x = Integer.parseInt(k.substring(i, i + 4));
            s.append(c[x]);
        }
        return s.toString();
    }
}
